import BaseEvent from '@/components/events/BaseEvent'
import { COMPONENT_TYPES, CONTROL_TYPES, COMPARE_FORM } from '@/common/const-wb'
import { getCompIdByParam, isJSON, isHasOwnProperty } from '@/plugins/utils'

export default class CompareInCompsEvent extends BaseEvent{
  constructor() {
    super();
  }

  excute({item, event, resetSubmitStatus}){
    this.item = item;
    this.event = event;
    this.resetSubmitStatus = resetSubmitStatus;
    //compareCompId 符号是-
    // compareInComps: [{"compId":"","compareCompId":"","compareRule":">10000"}]
    const compDataStr = event.comps[0].attrs.value;
    let isContinue = true
    let msg = '';
    let hint = '';
    let compDataJson;
    if(compDataStr && compDataStr.length > 0) {
      compDataJson = JSON.parse(compDataStr);
      hint = compDataJson.hint;
      const compId = getCompIdByParam(compDataJson.compId) || compDataJson.compId;
      const compData = this.componentMap[compId];
      const compareCompId = getCompIdByParam(compDataJson.compareCompId) || compDataJson.compareCompId;
      const compareCompData = this.componentMap[compareCompId];
      const compareCompIds = compDataJson.compareCompIds && compDataJson.compareCompIds.split(",")

      if(compareCompIds && compareCompIds.length > 0) {
        //1990
        let compareCompData = null;
        let compareId = '';
        for(let i = 0, len = compareCompIds.length; i < len; i++) {
          compareId = compareCompIds[i];
          compareCompData = this.componentMap[compareId];
          // 站点2476 截图数据校验
          if(compareCompData && compareCompData.cid == COMPONENT_TYPES.wb_camera){
            if(!compareCompData.commonAttr.cropImgUrl){
              isContinue = false
              msg = "亲，你还有没有选的哦！！！"
              break;
            }
          }else if (compareCompData && !this.formValues[compareCompData.id]
            || !this.formValues[compareCompData.id].value
            || this.formValues[compareCompData.id].value == compareCompData.commonAttr.placeholder
            ) {
              isContinue = false
              msg = "亲，你还有没有选的哦！！！"
              break;
            }
        }
      }else if(compData && compData.events.number && compareCompData && compareCompData.events.number){
        const numberControlData = this.controls[compId][CONTROL_TYPES.number];
        const numberMinusControlData = this.controls[compareCompId][CONTROL_TYPES.number];
        let compareResult =  this.compareInCompsIsContinue(numberControlData, numberMinusControlData, hint);
        isContinue = compareResult.isContinue;
        msg = compareResult.msg;
      }else if(compareCompData && compareCompData.cid == COMPONENT_TYPES.wb_calendar){
        //连续签到几天才能抽奖 {"signDay":1,"btnCompId":"05c6a3f2-3f7d-40c6-b286-2512c6aa98e0"}
        let voteControlData = this.controls[compareCompId][CONTROL_TYPES.vote];
        const interactionEvent = compareCompData.events.interactionData;
        const interactionStr = interactionEvent.comps[0].attrs.value;
        let interactionObj;
        if(interactionStr && isJSON(interactionStr)){
          interactionObj = JSON.parse(interactionStr)
        }
        let voteCount = 0;
        _.forEach(voteControlData.data.elements, (vote) => {
          if(vote.num > 0){
            voteCount++;
          }
        })
        if(voteCount < interactionObj.signDay){
          isContinue = false
          msg = "亲，签到天数不够！！！"
        }
      }else if(compareCompData && compareCompData.events.activityInfo){
        if(this.activityInfo.isJoin) {
          isContinue = false;
          msg = "亲，您以参加过了！！！"
        }
      }else if(compareCompData && compareCompData.events.timer){
        let end = 0
        let start = 0
        let now = 0
        let timerControlData = this.controls[compareCompId][CONTROL_TYPES.timer];
        if(timerControlData.data.time && timerControlData.data.time.length > 0){
          start = new Date(timerControlData.data.time).getTime()
          end = new Date(timerControlData.data.endTime).getTime()
          now = new Date().getTime()
        }
        let startTime = 0
        let endTime = 0
        startTime = now - start
        endTime = end - now
        if(startTime < 0) {
          isContinue = false
          msg = "亲，活动还未开始！！请耐心等待！！"
        }else if(endTime < 0){
          isContinue = false
          msg = "亲， 活动已结束了哦！！"
        }
      }else if(compareCompData && COMPARE_FORM.indexOf(compareCompData.cid) != -1 || compData && COMPARE_FORM.indexOf(compData.cid) != -1){
        let compareCompId = compareCompData.id;
        if(isHasOwnProperty(this.item, 'isInGroupCarousel') && this.item.isInGroupCarousel) {
          let idIndexs = this.item.id.split('_');
          compareCompId = compareCompId + '_' + idIndexs[1] + '_' + compareCompData.commonAttr.qIndex;
        }
        if(!this.formValues[compareCompId] || compData && !this.formValues[compData.id]){
          isContinue = false
          msg = "亲，你还有没有选的哦！！！"
        }else if(this.formValues[compareCompId]){
          // 对多选数据进行校验 站点2129&pageId=21
          let selectNum = this.formValues[compareCompId].selectNum
          if(selectNum){
            let min = compDataJson.min
            let max = compDataJson.max
            if(selectNum > max){
              isContinue = false
              msg = `亲，最多可选${ max }个`
            }
            if(selectNum < min){
              isContinue = false
              msg = `亲，至少选${ min }个`
            }
          }
        }
      }else if(compareCompData.cid == COMPONENT_TYPES.wb_camera){
        if(!compareCompData.commonAttr.cropImgUrl){
          isContinue = false
          msg = "亲，你还没有从相册中选择图片哦！！！";
        }
        if(compareCompData.commonAttr.isPoster){
          if(!compareCompData.commonAttr.isAddPoster){
            isContinue = false
            msg = "亲，你还没有选择贴纸哦！！";
          }
        }
      }else if(compareCompData.cid == COMPONENT_TYPES.wb_scroll_container || compareCompData.cid == COMPONENT_TYPES.wb_pixis){
        // 设置最小值或是最大值
        let min = compDataJson.min ? compDataJson.min : 3
        if(!this.formValues[compareCompData.id] || this.formValues[compareCompData.id].selecteds.length < min){
          isContinue = false
          msg = "亲，再选择几个呗！！！";
        }
        if(compDataJson.max){
          if(!this.formValues[compareCompData.id] || this.formValues[compareCompData.id].selecteds.length >= compDataJson.max){
            isContinue = false
            msg = `亲,最多只能选择${ compDataJson.max }个！！！`;
          }
        }
      }else if(compareCompData.cid == COMPONENT_TYPES.wb_text){
        if(compareCompData.commonAttr.isEdit){
          if(!this.formValues[compareCompData.id] || this.formValues[compareCompData.id].value == compareCompData.commonAttr.placeholder){
            isContinue = false
            msg = compareCompData.commonAttr.placeholder;
          }
        }else{
          let dataKey = compareCompData.commonAttr.dataKey;
          if(dataKey){
            let num = this.formValues[compareCompData.id].value;
            let rule = compDataJson.compareRule;
            let symbols = rule.split(',');
            if(symbols[0] == '>='){
              if(num >= parseInt(symbols[1])){
                isContinue = true
              }else{
                isContinue = false;
                msg = "亲，您的数量不够哦！！！";
              }
            }
          }
        }
      }else if(compareCompData && compareCompData.events.interactionData && compareCompData.interactionData){
        if(compareCompData.interactionData.cardMap) {
          //每一项都大于0, 每个卡片持有一个就可以合成了 站点1372 this.interactionData.card
          const cardMapControlData = this.controls[compareCompId][CONTROL_TYPES.wb_judge_right_wrong].data.cardMap;
          let total = 0;
          let length = 0;
          _.forEach(cardMapControlData, (num) => {
            length++;
            if(num > 0){
              total += 1;
            }
          })
          isContinue = total >= length ? true : false;
          if(!isContinue){
            msg = "亲，还不能合成，去收集全在来吧！！！";
          }
        }else if(compareCompData.interactionData.puzzleMap) {
          //this.interactionData.puzzle
          let puzzle = this.interactionData.puzzle;
          let matchLength = compareCompData.interactionData.puzzleMap.matchLength;
          let team_one = _.dropRight(puzzle, matchLength);
          let team_two = _.drop(puzzle, matchLength);
          let isOne = false;
          let isTwo = false;
          for(let i = 0, len = team_one.length; i < len; i++) {
            let one = parseInt(team_one[i]);
            let two = parseInt(team_two[i]);
            if(one == 1) {
              isOne = true;
            }
            if(two == 1) {
              isTwo = true
            }
          }
          if(isOne && isTwo) {
            isContinue = true
          } else {
            isContinue = false
          }
          if(!isContinue){
            msg = "亲，还不能翻牌，去收集全在来吧！！！";
          }
        }
      }else{
        //1471 新年宫气妆
        if(!this.formValues[compareCompData.id] || compData && !this.formValues[compData.id]) {
          isContinue = false
          msg = "亲，你还有没有选的哦！！！"
        }
      }
    }
    if(!isContinue){
      if(hint){
        msg = hint;
      }
      const popId = compDataJson && compDataJson.popId;
      const pageId = compDataJson && compDataJson.pageId;
      if(popId){
        this.showPop(popId);
      }
      if(pageId){
        this.showPage(pageId);
      }
      if(msg){
        this.showToast(msg);
      }
    }
    return isContinue;
  }
  //抽奖如果没有抽奖次数了，显示弹出是抽奖失败的弹层，但是文本是自定义的，如果没有自定义的文本就提示 亲，没有抽奖次数了！！
  compareInCompsIsContinue(numberControlData, numberMinusControlData, hint){
    let isContinue = true;
    let msg = '';
    if (numberMinusControlData && numberControlData && numberControlData.data.isUid) {
      if(numberMinusControlData.data.onceGrowValue < 0 || numberMinusControlData.data.op == '-'){
        const num = parseInt(numberControlData.data.num);
        const onceGrowValue = parseInt(numberMinusControlData.data.onceGrowValue);
        if (num - Math.abs(onceGrowValue) < 0) {
          msg = "亲，没有次数了！！！";
          if(hint){
            msg = hint;
          }
          this.resetSubmitStatus()
          isContinue = false;
        }
      }
    }
    return {isContinue , msg};
  }

  destroy(){
    this.item = null;
    this.event = null;
    this.isH5Edit = false;
    this.resetSubmitStatus = null;
  }

}
